#!/bin/bash


# main shell file for tauctionx project (dataverse)


# project directory
PROJECT_DIRECTORY=$(cd `dirname $0` && pwd)
echo "Project directory:"
echo ${PROJECT_DIRECTORY}


# options
prepare_data=1
run_empirical_analysis=1
estimate_model=1
summarize_model=1
run_model_analysis=1
compile_tables=1


##########################################################################################
# prepare input data (python)

if [[ ${prepare_data} -eq 1 ]]; then
    cd "${PROJECT_DIRECTORY}/empirical_analysis/py"
    
    echo "preparing TreasuryDirect auction data"
    python clean_treasurydirect.py
    
fi





##########################################################################################
# run all empirical analysis (stata)

if [[ ${run_empirical_analysis} -eq 1 ]]; then
    cd "${PROJECT_DIRECTORY}/empirical_analysis/do"
    
    echo "running main empirical analysis"
    stata-se -q -e run main_empirical
    
fi


##########################################################################################
# estimate model and summarize experiments (julia)

if [[ ${estimate_model} -eq 1 ]]; then
    cd "${PROJECT_DIRECTORY}/model_nkph/bin"
    
    # non-crisis period
    echo "estimating NKPH model: non-crisis"
    julia estimate_nkph_continuation.jl \
        --model_fname baseline_low_mmts \
        --rates_fname yield_macro_data \
        --regs_fname habitat_regressions_non_crisis \
        --alt_regs_fname alt_habitat_regressions_non_crisis \
        --rates_date_start 1986m6 \
        --rates_date_end 2007m1 \
        --seed 1234 \
        --draw_init_method 0 \
        --draw_init_std 0.0 \
        --N_steps 11 \
        --estimates_fname EST_baseline_low


    # crisis period
    echo "estimating NKPH model: crisis"
    julia estimate_nkph_continuation.jl \
        --model_fname baseline_high_mmts \
        --rates_fname yield_macro_data \
        --regs_fname habitat_regressions_crisis \
        --alt_regs_fname alt_habitat_regressions_crisis \
        --rates_date_start 2008m1 \
        --rates_date_end 2012m1 \
        --seed 1234 \
        --draw_init_method 0 \
        --draw_init_std 0.0 \
        --N_steps 11 \
        --estimates_fname EST_baseline_high
    
fi


if [[ ${summarize_model} -eq 1 ]]; then
    cd "${PROJECT_DIRECTORY}/model_nkph/bin"

    echo "summarizing NKPH model and running experiments"
    julia summarize_estimates_experiments.jl \
        --run_summary 1 \
        --run_baseline 1 \
        --run_robustness 1 \
        --N_param_vals_robustness 31
    
fi


##########################################################################################
# run all model analysis (stata)

if [[ ${run_model_analysis} -eq 1 ]]; then
    cd "${PROJECT_DIRECTORY}/empirical_analysis/do"
    
    echo "running main model analysis"
    stata-se -q -e run main_model
    
fi




##########################################################################################
# prepare final tables (python)

if [[ ${compile_tables} -eq 1 ]]; then
    cd "${PROJECT_DIRECTORY}/empirical_analysis/do"
    
    # combined auction summary and shock statistics table
    echo "auction summary tables"
    python tauctionx_process_tex_tables.py t1ab_auc_shock_summary_pyedit


    # combined demand measure regressions table
    echo "auction demand regression tables"
    python tauctionx_process_tex_tables.py t1c_auc_demand_combined_pyedit


    # combined asset price regression tables (OLS and IV)
    echo "asset price demand shock regression tables"
    python tauctionx_process_tex_tables.py t2a_asset_price_combined_pyedit
    python tauctionx_process_tex_tables.py t2b_iv_asset_price_combined_pyedit


    # combined private localization table
    echo "asset price localization regression tables"
    python tauctionx_process_tex_tables.py t3e_ph_regression_combined_pyedit


    # combined model calibration table
    echo "model calibration table"
    python tauctionx_process_tex_tables.py calibration_moments_pyedit
    
fi



